package com.cloudcc.mobile.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudcc.cloudframe.util.GsonUtil;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.easemob.util.NetUtils;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.litesuits.android.log.Log;
import com.mypage.model.ContactDelece;
import com.mypage.utils.AnimViewUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NewcontactDelete extends BaseActivity {

    @Bind(R.id.deletePerson)
    TextView deletePerson;
    @Bind(R.id.cancelBtn)
    TextView cancelBtn;
    @Bind(R.id.shanchu_title)
    TextView shanchu_title;
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_group);
        ButterKnife.bind(this);
        shanchu_title.setText(getResources().getString(R.string.contactquerenshanchu));
        initdata();

    }
    @Override
    public int getLayoutId() {
        return R.layout.activity_delete_group;
    }
    private void initdata() {
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        recordId = intent.getStringExtra("recordId");
        layoutId = intent.getStringExtra("layoutId");
        relatedlistId = intent.getStringExtra("relatedlistId");
        lianxiren_name = intent.getStringExtra("lianxirenname");
    }
    private RelativeLayout topLayoutBackg;
    private TextView toastHintContent;
    private ImageView imgDeletes;
    FrameLayout layoutTop;
private boolean iscanle = false;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        RunTimeManager.getInstance().setIsrem(false);
        finish();
        return super.onTouchEvent(event);
    }
    @OnClick({R.id.deletePerson, R.id.cancelBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.deletePerson:
                iscanle = false;
                if(NetUtils.hasNetwork(NewcontactDelete.this)){
                    request();
                }else{
                    layoutTop = (FrameLayout) findViewById(R.id.layoutTop);
                    toastHintContent = (TextView) findViewById(R.id.toastHintContent);
                    imgDeletes = (ImageView) findViewById(R.id.imgDeletes);
                    topLayoutBackg = (RelativeLayout) findViewById(R.id.topLayoutBackg);
                    topLayoutBackg.setBackground(getResources().getDrawable(R.drawable.toast_hint_red));
                    toastHintContent.setText(getString(R.string.meg_net_no));
                    topLayoutBackg.setVisibility(View.GONE);
                    layoutTop.setVisibility(View.VISIBLE);
                    new AnimViewUtils().appearToast(topLayoutBackg);
                }

                break;
            case R.id.cancelBtn:
                iscanle = true;

                finish();
                break;
        }
    }
    private String  recordId,layoutId,relatedlistId,lianxiren_name;
    private void request(){
    HttpUtils utils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addBodyParameter("binding", RunTimeManager.getInstance()
                .getServerBinding());
        params.addBodyParameter("serviceName", "deleteOpportunityRole");
        params.addBodyParameter("opportunityRoleId",id);
        Log.d("request",
                "链接" + UrlManager.getInterfaceUrl()
                        + GsonUtil.Object2Json(params));
        utils.send(HttpRequest.HttpMethod.POST, UrlManager.getInterfaceUrl(), params,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        Log.d("-------------------------", "Failure。。。"
                                + arg1);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        String result = arg0.result;
                        Log.d("request",
                                "Success_down:     " + result);
                        Gson gson = new Gson();
                        ContactDelece ss = gson.fromJson(result,
                                ContactDelece.class);
                        if("true".equals(ss.result)&&"1".equals(ss.returnCode)){
                            NewContact.instance.finish();
                            Intent intent = new Intent(NewcontactDelete.this,NewContact.class);
                            intent.putExtra("recordId",recordId);
                            intent.putExtra("layoutId",layoutId);
                            intent.putExtra("relatedlistId",relatedlistId);
                            intent.putExtra("tag","shanchu");
                            intent.putExtra("name",lianxiren_name);
                            startActivity(intent);
                        }
                        NewContactDetails.instance.finish();
                        finish();
//                        {"result":true,"returnInfo":"","returnCode":"1","data":null}
//                        Gson gson = new Gson();
//                        newcowerEntity ss = gson.fromJson(result,
//                                newcowerEntity.class);

                    }
                });
    }

    @Override
    public void finish() {
        super.finish();
        if (iscanle) {
            this.overridePendingTransition(R.anim.activity_close, 0);
        }
    }
}
